const Controllers = require('../controllers/Controller.js').controller;
const app = getApp();

var startPoint
Page({
  data: {
    globalStype:'',
    isFixedTop:false,
    tabbar:[],
    cloutGoodsPagedata:[],
    pageNumber:1,
    pageSize:10,
    isEnd:false,
    isloading:true,
    malllists:[
      {
        name:'云商城',
        id:''
      }
    ],
    shopId:'',
    mallindex:0,
    clickMe:true,
    buttonRight:0,
    buttonBottom:0
  },

   //以下是按钮拖动事件
  buttonStart: function (e) {
    startPoint = e.touches[0]//获取拖动开始点
  },
  buttonMove: function (e) {
    var endPoint = e.touches[e.touches.length - 1]//获取拖动结束点
    //计算在X轴上拖动的距离和在Y轴上拖动的距离
    var translateX = endPoint.clientX - startPoint.clientX
    var translateY = endPoint.clientY - startPoint.clientY
    startPoint = endPoint//重置开始位置
    var buttonBottom = this.data.buttonBottom - translateY
    var buttonRight = this.data.buttonRight - translateX
    //判断是移动否超出屏幕
    if (buttonRight+50 >= this.data.windowWidth){
      buttonRight = this.data.windowWidth-50;
    }
    if (buttonRight<=0){
      buttonRight=0;
    }
    if (buttonBottom<=0){
      buttonBottom=0
    }
    if (buttonBottom + 50 >= this.data.windowHeight){
      buttonBottom = this.data.windowHeight-50;
    }
    this.setData({
      buttonBottom: buttonBottom,
      buttonRight: buttonRight
    })
  },
  buttonEnd: function (e) {
  },


  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
    this.setData({
      tabbar: this.data.globalStype=='pinStyle'?app.globalData.tabbar:app.globalData.tabbar2,
    })
    if(app.globalData.shopId){
      this.setData({
        shopId:app.globalData.shopId,
      })
      // this.selectLabel()
      this.cloutGoodsPage(this.data.malllists[0].id)
    }
    let that=this
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight:  res.windowHeight,
          windowWidth:  res.windowWidth,
          buttonBottom:250,//这里定义按钮的初始位置
          buttonRight:0,//这里定义按钮的初始位置
        })
      }
    })
  },
  tabChange(e){
    var index=e.detail.index;
    if(index==0){

      wx.reLaunch({
        url: "/pages/Index/Index"
      });

      // wx.requestSubscribeMessage({
      //   tmplIds:['CgSkX1UN123rlfAQ9zXiMyfiF1ugDkOsd8yLmvcbHt0','IoewIlKNaUfOLOhKUOdPKMOnJxdtdqWGfOotuOB2R70','TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'],//'TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'
      //   success:res=>{
      //     console.log(res)
      //   },
      //   complete:()=>{
      //     wx.reLaunch({
      //       url: "/pages/Index/Index"
      //     });
      //   }
      // })


    }
    if(index==2){
      wx.reLaunch({
        url: "/pages/TradingArea/TradingArea"
      });
    }
    if(index==3){
      wx.reLaunch({
        url: "/pages/couponBagCenter/couponBagCenter"
      });
    }
    if(index==4){
      wx.reLaunch({
        url: "/pages/my/my-index/my-index"
      });
    }
  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    console.log(e.scrollTop)
    let query = wx.createSelectorQuery()
    query.select('#malllists').boundingClientRect((rect) => {
      if(rect){
        let top = rect.top;
        if (top < e.scrollTop - 64) {
          this.setData({
            isFixedTop: true
          })
        } else {
          this.setData({
            isFixedTop: false
          })
        }
      }
    }).exec()
  },
  // selectLabel(){
  //   Controllers.selectLabel({type:1,shopId:this.data.shopId}).then(res => {
  //     this.setData({
  //       malllists:res.data.concat(this.data.malllists)
  //       // malllists:this.data.malllists.concat(res.data)
  //     })
  //     this.setData({
  //       labelId:this.data.malllists[0].id
  //     })
  //     this.cloutGoodsPage(this.data.malllists[0].id)
  //   });
  // },
  cloutGoodsPage(labelId){
    if(this.data.labelId){
      Controllers.labelGoodsPage({
        pageSize:this.data.pageSize,
        pageNumber:this.data.pageNumber,
        shopId:this.data.shopId,
        labelId:this.data.labelId
      }).then(res => {
        if(res.data.list.length>0){
            for(let item of res.data.list){
              if(item.label){
                item.label=item.label.split('/')
              }
            }
          this.setData({
            cloutGoodsPagedata:this.data.cloutGoodsPagedata.concat(res.data.list),
  
          })
          
        }
        if(res.data.lastPage){
            this.setData({
              isEnd:true
            })
          }
        this.setData({
          isloading:false
        })
      }).catch(res=>{
        this.setData({isloading:false})
      })
    }else{
      Controllers.cloutGoodsPage({
        pageSize:this.data.pageSize,
        pageNumber:this.data.pageNumber,
        shopId:this.data.shopId
      }).then(res => {
        if(res.data.list.length>0){
            for(let item of res.data.list){
              if(item.label){
                item.label=item.label.split('/')
              }
            }
          this.setData({
            cloutGoodsPagedata:this.data.cloutGoodsPagedata.concat(res.data.list),
  
          })
          
        }
        if(res.data.lastPage){
            this.setData({
              isEnd:true
            })
          }
        this.setData({
          isloading:false
        })
      }).catch(res=>{
        this.setData({isloading:false})
      })
    }
    
  },
  
  onReachBottom() {
    if (this.data.isEnd) {
      return;
    } else {
      this.setData({
          pageNumber: this.data.pageNumber + 1,
      });
      this.cloutGoodsPage()
    }
  },
  changemall(e){
    this.setData({
      cloutGoodsPagedata:[],
      pageNumber:1,
      pageSize:10,
      isEnd:false,
      isloading:true,
      mallindex:e.currentTarget.dataset.index,
      shopId:this.data.shopId,
      labelId:e.currentTarget.dataset.id
    })
    this.cloutGoodsPage(e.currentTarget.dataset.id)
  },
  //获取通知权限
  getNotice(){
    // wx.requestSubscribeMessage({
    //   tmplIds:['CgSkX1UN123rlfAQ9zXiMyfiF1ugDkOsd8yLmvcbHt0','IoewIlKNaUfOLOhKUOdPKMOnJxdtdqWGfOotuOB2R70','DlEG_b9R6f2bWjrW-jEmsoy_hcSHMMB9TPhh9q9GbCo'],
    //   success:res=>{
    //     console.log(res)
    //   },
    //   complete:()=>{
    //     wx.navigateTo({
    //       url:"/pages/Mall/Coupon"
    //     })
    //   }
    // })

    wx.navigateTo({
      url:"/pages/Mall/Coupon"
    })
  },


  toClickMe(){
    wx.navigateTo({
      url: '/pages/clickMe/clickMe',
    })
  },

  cancleClickMe(){
    this.setData({
      clickMe:false
    })
  }
})